﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

public class GameSelect : MonoBehaviour {
    public ScrollRect ScrollRect;
    public Transform ScrpllParent;
    public AppButton AppButton;
    public StartScript script;
	// Use this for initialization
	void Start () {
        string path = Application.streamingAssetsPath + "/LAVA";
        if (Application.platform == RuntimePlatform.PSP2)
        {
            path = "ux0:data/LAVA";
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
        }
        CreateButtons(path);
        string[] directories = Directory.GetDirectories(path);
        for (int i = 0; i < directories.Length; i++)
        {
            CreateButtons(directories[i]);
        }
       
    }

    private void CreateButtons(string path)
    {
        string[] files = Directory.GetFiles(path, "*.lav");
        for (int i = 0; i < files.Length; i++)
        {
            Create(files[i]);
        }
    }

    public void Create(string path)
    {
        string name= Path.GetFileName(path);
        string dir = Path.GetDirectoryName(path);
        AppButton button = Instantiate<AppButton>(AppButton, ScrpllParent);
        Text text = button.transform.GetComponentInChildren<Text>();
        text.text = name.Replace(".lav","");
        Button btn = button.GetComponent<Button>();
        button.RootPath = dir;
        btn.onClick.AddListener(() =>{
            script.appName = button.transform.GetComponentInChildren<Text>().text;
            script.gameObject.SetActive(true);
            script.RootPath = button.RootPath;
            gameObject.SetActive(false);
        });
    }
	
	// Update is called once per frame
	void Update () {
		
	}
}
